package com.liuyang.demo.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;


public class ThreadPoolDemo {

    public static final ExecutorService THREAD_POOL;

    static {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("my-thread-pool-%d").build();
        THREAD_POOL = new ThreadPoolExecutor(10, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    public static void main(String[] args) {
        System.out.println("一：" + Thread.currentThread().getName());

        try {
            THREAD_POOL.execute(() -> doExecute());
        } finally {
            System.out.println("三：" + Thread.currentThread().getName());
            THREAD_POOL.shutdown();
            System.out.println("四：" + Thread.currentThread().getName());
        }

        System.out.println("五：" + Thread.currentThread().getName());

/*
一：main
三：main
四：main
五：main
二：my-thread-pool-0
*/
    }

    public static void doExecute() {
        System.out.println("二：" + Thread.currentThread().getName());
    }
}
